
/*
 *  Copyright 2007 Marc Lanctot and Marc Gendron-Bellemare
 *
 *  This file is part of OpenMTG.
 *
 *  OpenMTG is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenMTG is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenMTG.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


import java.io.*;
import java.util.*;

public class Deck {

  private ArrayList<Card> cards;

  public Deck(String deckFilePath){
    // fill in our cards list from reading a .deck file
    try{
      cards=new ArrayList<Card>(Constants.DEFAULT_DECK_SIZE);
      BufferedReader input = new BufferedReader(new FileReader(deckFilePath));

      String line=input.readLine();
      while(line!=null){
        String splitted[]=line.split(",");

        for(int i=0; i<Integer.parseInt(splitted[0]); i++){
          cards.add( Card.int2Card(
                       Integer.parseInt(splitted[2])*Constants.MAX_SET_NUM
                       +Integer.parseInt(splitted[3])
                       ) );
        }

        line=input.readLine();
      }
    } catch(IOException e){
      System.err.println("e");
      System.exit(-1);
    }


  }

  public void init(int oid) {
    for (Card c : cards)
      c.init(oid);
  }

  public int size(){
    return cards.size();     // TODO: double check that arraylist has this (this should return the number of objects actually *there*, not the size of the array held inside the arraylist)
  }

  // TODO: remove front the front or from the end?
  public Card deal(){
    Card theCard=null;
    theCard=cards.remove(0);     // TODO: i forget if this is ok to do..
    return theCard;
  }

  // TODO: verify this is the most efficient way to do this :P
  public void swap(int pos1, int pos2){
    Card temp = cards.get(pos2);
    cards.set( pos2, cards.get(pos1) );
    cards.set( pos1, temp );
  }

  public void putOnBottom(Card c)
  {
    cards.add(c);
  }

  public void putOnBottom(ArrayList<Card> cards)
  {
    for (Card c : cards)
      putOnBottom(c);
  }


  public String toString(){
    String theString="";
    int i=0;
    for (Card c : cards){
      theString+=i+" ";
      theString+=c.toString()+"\n";
      i++;
    }
    return theString;
  }

}
